<template>
  <div id="detail">
    <DetailNavbar
      id="detail-navbar"
      @titleClick="titleClick"
      ref="navbar"
    ></DetailNavbar>
    <Scroll
      class="wrapper"
      ref="scroll"
      :probeType="3"
      :pullUpLoad="true"
      @scroll="contentScroll"
    >
      <DetailSwiper :topImages="topImages"></DetailSwiper>
      <DetailBaseInfo :goods="goods"></DetailBaseInfo>
      <DetailShopInfo :shop="shop"></DetailShopInfo>
      <DetailGoodsInfo
        :detailInfo="detailInfo"
        @imageLoad="imageLoad"
      ></DetailGoodsInfo>
      <DetailParamInfo ref="params" :paramInfo="paramInfo"></DetailParamInfo>
      <DetailCommentInfo
        ref="comment"
        :commentInfo="commentInfo"
      ></DetailCommentInfo>
      <GoodsList ref="recommend" :goods="recommends"></GoodsList>
    </Scroll>
    <DetailBottomBar @addToCart="addToCart"></DetailBottomBar>
    <BackTop v-show="isShowBackTop" @click.native="backTop"></BackTop>
  </div>
</template>

<script>
import DetailNavbar from "./childComps/DetailNavbar";
import DetailSwiper from "./childComps/DetailSwiper";
import DetailBaseInfo from "./childComps/DetailBaseInfo";
import DetailShopInfo from "./childComps/DetailShopInfo";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
import DetailParamInfo from "./childComps/DetailParamInfo";
import DetailCommentInfo from "./childComps/DetailCommentInfo";
import DetailBottomBar from "./childComps/DetailBottomBar";

import GoodsList from "@/components/content/goods/GoodsList.vue";

// import bus from "../../router/eventBus";
import {
  getDetail,
  Goods,
  Shop,
  GoodsParam,
  getRecommend,
} from "network/detail";
import { debounce } from "../../common/utils";
export default {
  name: "Detail",
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: {},
      themeTopY: [],
      isShowBackTop: false,
    };
  },
  components: {
    DetailNavbar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar,
  },
  methods: {
    imageLoad() {
      this.$refs.scroll.scroll.refresh();
      this.getThemeTopY();
    },
    titleClick(index) {
      this.$refs.scroll.scroll.scrollTo(0, -this.themeTopY[index], 400);
    },
    contentScroll(position) {
      const positionY = -position.y;
      let index = 0;
      for (let i = 0; i < this.themeTopY.length; i++) {
        if (
          index !== i &&
          ((positionY >= this.themeTopY[i] &&
            positionY < this.themeTopY[i + 1]) ||
            (i === this.themeTopY.length - 1 && positionY >= this.themeTopY[i]))
        ) {
          index = i;
          this.$refs.navbar.currentIndex = index;
        }
      }
      position.y < -1000
        ? (this.isShowBackTop = true)
        : (this.isShowBackTop = false);
    },
    backTop() {
      this.$refs.scroll.scroll.scrollTo(0, 0, 1000);
    },
    addToCart() {
      // 1.获取购物车需要展示的信息
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.oldPrice;
      product.checked = this.goods.checked;
      product.iid = this.iid;
      // 2.将商品加入购物车界面
      this.$store.commit("addToCart", product);
      // 3.添加到购物车成功
      this.message = "添加成功，在购物车等亲";
    },
  },
  created() {
    // 1.保存传入的 iid
    this.iid = this.$route.params.iid;
    // 2.根据iid请求详情数据
    getDetail(this.iid)
      .then((res) => {
        // 获取顶部的图片轮播数据
        let data = res.data.result;
        this.topImages = data.itemInfo.topImages;
        // 获取商品信息
        this.goods = new Goods(
          data.itemInfo,
          data.columns,
          data.shopInfo.services
        );
        // 获取店铺信息
        this.shop = new Shop(data.shopInfo);
        // 获取商品详情信息
        this.detailInfo = data.detailInfo;
        // 获取参数的信息
        this.paramInfo = new GoodsParam(
          data.itemParams.info,
          data.itemParams.rule
        );
        // 获取评论信息
        if (data.rate.cRate !== 0) {
          this.commentInfo = data.rate.list[0];
        }
        // 给themeTopY 进行赋值，对赋值操作进行防抖
        this.getThemeTopY = debounce(() => {
          // 商品的offsetTop
          this.themeTopY.push(0);
          // 参数的offsetTop
          this.themeTopY.push(this.$refs.params.$el.offsetTop);
          // 评论的offsetTop
          this.themeTopY.push(this.$refs.comment.$el.offsetTop);
          // 推荐的offsetTop
          this.themeTopY.push(this.$refs.recommend.$el.offsetTop);
        });
      })
      .catch((err) => {
        console.log(err);
      });
    // this.getDetail(this.iid);
    // this.getRecommend();
    getRecommend().then((res) => {
      this.recommends = res.data.data.list;
    });
  },
};
</script>

<style lang="less" scoped>
#detail {
  position: relative;
  background-color: #fff;
  z-index: 9;
  height: 100vh;
  #detail-navbar {
    position: relative;
    top: 0;
    left: 0;
    right: 0;
    z-index: 99;
    background-color: #fff;
  }
  .wrapper {
    height: calc(100% - 93px);
    overflow: hidden;
  }
}
</style>
